/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/8/2 12:08 上午
 * @version V1.0
 */

import { pathToRegexp } from "path-to-regexp";
import { message } from "antd";
import { getChildCascader } from "utils/index";
import {
    createLeaderUser,
    deleteLeaderUser,
    getDepartmentTreeList,
    getLeaderUserModelList,
    changeLeaderUserInfo
} from "api/index";

export default {
    namespace: "leaders",
    state: {
        dataSource: [],
        keyword: "",
        modalTitle: "新增领导",
        modalVisible: false,
        itemData: {},
        treeData: []
    },
    reducers: {
        setDataSource(state, { payload: { dataSource, keyword } }) {
            return {
                ...state,
                dataSource,
                keyword
            };
        },
        setModalVisible(state, { payload: modalVisible }) {
            return { ...state, modalVisible };
        },
        setModalTitle(state, { payload: modalTitle }) {
            return { ...state, modalTitle };
        },
        setItemData(state, { payload: itemData }) {
            return { ...state, itemData };
        },
        setUnitsTreeData(state, { payload: treeData }) {
            return { ...state, treeData };
        }
    },
    effects: {
        /**
         * @desc 获取领导列表
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getDataSource({ payload }, { call, put, select }) {
            const result = yield call(getLeaderUserModelList, payload);
            if (result.status === "success") {
                const dataSource = result.result;
                yield put({
                    type: "setDataSource",
                    payload: {
                        dataSource,
                        keyword: payload.keyword
                    }
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 获取单位
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getUnitsTreeData({ payload }, { call, put, select }) {
            const result = yield call(getDepartmentTreeList, payload);
            if (result.status === "success") {
                const data = result.result;
                const treeData = data.map(c => {
                    if (!c.hasLowerDepartment) {
                        return {
                            value: c.departmentId,
                            label: c.departmentName
                        };
                    } else {
                        return {
                            value: c.departmentId,
                            label: c.departmentName,
                            children: getChildCascader(c.lowerDepartmentModel)
                        };
                    }
                });
                yield put({
                    type: "setUnitsTreeData",
                    payload: treeData
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 添加领导
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* addLeaderUser({ payload, callback }, { call, put, select }) {
            const result = yield call(createLeaderUser, payload);
            if (result.status === "success") {
                message.success(`添加${payload.realName}成功`);
                if (callback && typeof callback === "function") {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 删除领导
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* removeLeaderUser({ payload, callback }, { call, put, select }) {
            const result = yield call(deleteLeaderUser, payload);
            if (result.status === "success") {
                message.success(`删除成功`);
                if (callback && typeof callback === "function") {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 修改领导
         * @param payload
         * @param callback
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* updateLeaderUserInfo({ payload, callback }, { call, put, select }) {
            const result = yield call(changeLeaderUserInfo, payload);
            if (result.status === "success") {
                message.success(`修改${payload.realName}成功`);
                if (callback && typeof callback === "function") {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        }
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathToRegexp("/users/leaders").exec(pathname)) {
                }
            });
        }
    }
};
